import { Component, Host, ChangeDetectorRef, ViewChild } from '@angular/core';
import { environment } from '../../../environments/environment';
import { AppComponent } from '../../app.component';
import { PortfolioSharingStatus } from './models/portfolioSharingStatus.model';
import { ModalComponent } from '../../shared/components/modal/modal.component';
import { PortfolioType } from './models/portfolioType.model';
import { PortfolioListComponent } from './portfolioList/portfolioList.component';
import { StorageService } from '../../shared/services/storage.service';
import { BaseComponent } from '../../shared/components/baseComponent/base.component';
import { PortfolioService } from './portfolio.service';
import { PortfolioMonitoringStatusModel } from './models/portfolioMonitoringStatus.model';


/**
 * Portfolio main component
 *
 * Author: Mihai Stanescu
 * Date: 19.04.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'portfolio.component.html',
    styleUrls: ['./portfolio.component.scss'],
    providers: [PortfolioService]
})
export class PortfolioComponent extends BaseComponent {
    public readonly resourcesUrl: string = environment.resourcesUrl;
    public sharedPortfolioStatus = PortfolioSharingStatus.Shared;
    public privatePortfolioStatus = PortfolioSharingStatus.Private;
    public allPortfolioStatus = PortfolioSharingStatus.All;
    public systemPortfolio = PortfolioType.System;
    public customPortfolio = PortfolioType.Custom;

    //User Info
    public hasMonitoringClaim: boolean;
    public userId: string;
    public accountId: string;

    public monitoringStatus: PortfolioMonitoringStatusModel /* = new PortfolioMonitoringStatusModel() */;

    //Monitor all

    @ViewChild('allPortfolioList') allPortfolioList: PortfolioListComponent;
    @ViewChild('sharePortfoliosList') sharePortfoliosList: PortfolioListComponent;
    @ViewChild('systemPortfolioList') systemPortfolioList: PortfolioListComponent;

    @ViewChild('portfolioCreationModal') portfolioCreationModal: ModalComponent;

    // Portfolio
    constructor(
        @Host() private rootComponent: AppComponent,
        private portfolioService: PortfolioService,
        private cdRef: ChangeDetectorRef) {
        super(cdRef);
    }

    ngOnInit() {
        this.rootComponent.finishLoading();

        //User credentials should be refactored to be passed from Portfolio page to child components
        let userInfo = StorageService.get().userInfo;
        this.userId = userInfo.userId;
        this.accountId = userInfo.accountId;
        this.hasMonitoringClaim = this.hasPermission('api/v1/subscribetoportfolio', 'PUT', [{ key: 'alert', value: 'quarterly' }]);

        this.getMonitoringStatus();
    }

    public reloadPortfolios() {
        this.allPortfolioList.getBulkPortfolios();
        this.sharePortfoliosList.getBulkPortfolios();
        this.systemPortfolioList.getBulkPortfolios();
        this.getMonitoringStatus();
    }

    //To implement
    public hasPortfolioPermision: boolean = true;

    public openAddPortfolioModalWindow() {
        this.portfolioCreationModal.show();
    }

    public portfolioMonitoringAllClick() {        
        let portfolioInfo = {
            portfolio : { id: this.monitoringStatus.portfolioMonitorAllId},
            isChecked: this.monitoringStatus.isMonitoringAll
        };
        this.portfolioService.modifyPortfolioSubscription(portfolioInfo, this.monitoringStatus, this.userId, this.accountId).subscribe(
            response => {
                if(response != null) this.rootComponent.showSuccess(response.message, "Success!"); 
                this.getMonitoringStatus();
                this.reloadPortfolios();
            }
        )
    }

    private getMonitoringStatus() {
        this.portfolioService.getPortfolioMonitoring(this.userId, this.accountId).subscribe(response => {
            this.monitoringStatus = response;
        });
    }
}